// pages/home/components/modelstyle/modelydl/modelydl.js
var app = getApp();
Component({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
 attached() {
    app.func.getPromise('/list/shortcut')
        .then(([code, res]) => {
            this.setData({brand: res.data});
        });


       app.func.getPromise('/list/zone')
    .then(([code, res]) => {
        this.setData({zone: res.data});
        let arr =[]
        
        this.data.zone.forEach((item,index) =>{
         
          arr.push(item.sku)
          this.setData({
            sku:arr
          });
        })
    })
   
    
        app.func.getPromise('/categoriesv2')
            .then(([code, res]) => {
                console.log(res);
                if (code == 200) {
                    this.Cart = res.data;
                    let leftmun = this.Cart.map(v => v.name);
                    let currentIndex = 0;
                    // for(let i = 0; i < this.Cart.length;i++) {
                    //     if(this.Cart[i].id == id) {
                    //         currentIndex = i;
                    //     }
                    // }
                    let rightcon = this.Cart[0].subitems;
                    let idset = rightcon[2].id;
                    this.setData({
                        currentIndex,
                        leftmun,
                        rightcon,
                        idset,
                    })
                }
            });
            app.func.postPromise('/categorySku', {
              id: 385,
              offset: 0,
              length: 4,
              sort: 0,
            }).then(res=>{
              console.log(res)
              this.setData({
                goodsList3 :res[1].data
              })
            })
            wx.getSystemInfo({
              success:  (res) => {
                console.log(res.windowWidth)
                let widhtradiv= res.windowWidth*0.9
                this.setData({
                  widhtradiv,
                })
               
              }
            })
},



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  methods: {
    brandsku: function (e) {
			let item = e.currentTarget.dataset.item;
			switch (item[`link_type`]){
				case 0:
					let id = item[`category_id`];
					// wx.setStorage({key:"ca【【【【tegory_id", id})旧
					wx.setStorageSync('category_id', id)
					let extConfig = wx.getExtConfigSync? wx.getExtConfigSync(): {} ;
					if(extConfig.category = null || extConfig.category == 0) {
						wx.navigateTo({
							url: '/pages/index/categorysku/categorysku',
						})
					} else {
						wx.navigateTo({
							url: '/pages/index/newcategory/newcategory?id=' + id,
						})
					}
					break;
				case 1:
					wx.navigateTo({
						url:'/pages/index/brandsku/brandsku?id=' + item['topic_id']
					})
					break;
				case 2:
					break;
			}
    },
    productdetail: function (e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/index/productdetail/productdetail?id=' + id
      })
    },
    addcart:function(e){
        let id = e.currentTarget.dataset.id;
            app.func.postPromise('/addcartv2',
                    {
                        pro_id:id,
                        count:1
                    })  .then(([code, res]) => {
                        if(code == 200){
                            app.func.toastPromise('加入购物车成功','none',  500)
                            .then(()=>{
                          
                            })
                            this.setData({
                              noaddcart:false
                            })
                        }else{
                            wx.showToast({
                              title:res.message,
                              icon:'none',
                              mask:true
                            })
                        }
                    })
                   
    },
    productdetail: function (e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/index/productdetail/productdetail?id=' + id
      })
    },
    
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})